<template>
  <div id="app">
    <!-- 主容器 -->
    <div class="main-container">
      <!-- 顶部导航 -->
      <div class="top-nav">
        <div class="nav-logo">
          <i class="fa-solid fa-paper-plane"></i>
          <span>小红书运营工具</span>
        </div>

        <div class="nav-right">
          <router-link class="login-link" to="/login"><i class="fa-solid fa-user"></i> 已有账号？登录</router-link>
        </div>
      </div>

      <!-- 主内容 -->
      <div class="main-content">
        <!-- 背景装饰 -->
        <div class="bg-decoration">
          <div class="bg-circle bg-circle-1"></div>
          <div class="bg-circle bg-circle-2"></div>
        </div>

        <!-- 注册卡片 -->
        <div class="register-card">
          <div class="card-header">
            <div class="card-title">创建新账户</div>
            <div class="card-subtitle">加入我们，开启您的创作之旅</div>
          </div>

          <!-- 社交登录 -->
          <div class="social-login">
            <div class="social-btn weixin">
              <i class="fa-brands fa-weixin"></i>
            </div>
            <div class="social-btn weibo">
              <i class="fa-brands fa-weibo"></i>
            </div>
            <div class="social-btn qq">
              <i class="fa-brands fa-qq"></i>
            </div>
            <div class="social-btn alipay">
              <i class="fa-brands fa-alipay"></i>
            </div>
          </div>

          <!-- 分割线 -->
          <div class="divider">
            <div class="divider-line"></div>
            <div class="divider-text">或者使用邮箱注册</div>
            <div class="divider-line"></div>
          </div>

          <!-- 注册表单 -->
          <el-form ref="registerForm" :model="formData" :rules="rules" class="register-form" label-position="top">
            <el-form-item label="用户名" prop="nickname">
              <el-input v-model="formData.nickname" placeholder="请输入用户名"></el-input>
            </el-form-item>

            <el-form-item label="手机号" prop="phone">
              <el-input v-model="formData.phone" placeholder="请输入手机号"></el-input>
            </el-form-item>

            <el-form-item label="邮箱" prop="email">
              <el-input v-model="formData.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input v-model="formData.password" type="password" placeholder="请输入密码"></el-input>
            </el-form-item>

            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input v-model="formData.confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
            </el-form-item>

            <el-form-item prop="agreement">
              <el-checkbox v-model="formData.agreement">
                我已阅读并同意
                <router-link to="/agreement/user" class="terms-link">用户协议</router-link>
                和
                <router-link to="/agreement/privacy" class="terms-link">隐私政策</router-link>
              </el-checkbox>
            </el-form-item>

            <el-form-item>
              <button class="register-btn" @click="handleRegister" :disabled="isLoading">
                <span v-if="!isLoading">立即注册</span>
                <span v-else>
                  <i class="fa-solid fa-spinner fa-spin"></i> 注册中...
                </span>
              </button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "registration-view",
  data() {
    return {
      formData: {
        nickname: '',
        phone: '',
        email: '',
        password: '',
        confirmPassword: '',
        agreement: false
      },
      rules: {
        nickname: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 20, message: '用户名长度在2-20个字符之间', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 20, message: '密码长度在6-20个字符之间', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '请确认密码', trigger: 'blur'},
          {validator: this.validateConfirmPassword, trigger: 'blur'}
        ],
        agreement: [
          {required: true, message: '请阅读并同意用户协议和隐私政策', trigger: 'change'}
        ]
      },
      isLoading: false
    };
  },
  methods: {
    // 验证确认密码
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.formData.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    },
    // 处理注册
    async handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          this.isLoading = true;

          try {
            // 准备请求数据
            const requestData = {
              nickname: this.formData.nickname,
              email: this.formData.email,
              phone: this.formData.phone,
              password: this.formData.password,
              confirmPassword: this.formData.confirmPassword
            };
            // 调用注册API
            const response = await this.$axios.post('/api/register', requestData);
            if (response.status !== 200) {
              // 注册成功处理
              this.$message.success('注册成功');
              this.$refs.registerForm.resetFields();
              // 跳转到登录页面
              await this.$router.push('/login');
            } else {
              // 注册失败处理
              this.$message.error('注册失败');
            }
          } catch (error) {
            // 错误处理
            let errorMessage = '注册失败';
            if (error.response) {
              errorMessage = error.response.data.error || errorMessage;
            }
            this.$message.error(errorMessage);
          } finally {
            this.isLoading = false;
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
/* 全局样式 */
body {
  font-family: 'Inter', system-ui, sans-serif;
  margin: 0;
  padding: 0;
  background-color: #F8F9FA;
  color: #222222;
  overflow-x: hidden;
}

/* 自定义变量 */
:root {
  --primary-color: #FE2C55;
  --secondary-color: #FF7D94;
  --dark-color: #222222;
  --light-color: #F8F8F8;
  --gray-color: #999999;
  --success-color: #4CD964;
  --warning-color: #FF9500;
  --info-color: #3399FF;
  --transition-duration: 0.3s;
}

/* 主容器 */
.main-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 顶部导航 */
.top-nav {
  padding: 20px 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 10;
}

.nav-logo {
  color: var(--primary-color);
  font-size: 24px;
  font-weight: bold;
  display: flex;
  align-items: center;
}

.nav-logo i {
  margin-right: 10px;
}

.nav-right {
  display: flex;
  align-items: center;
}

.login-link {
  color: var(--primary-color);
  font-weight: 500;
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 4px;
  transition: all var(--transition-duration) ease;
}

.login-link:hover {
  background-color: rgba(254, 44, 85, 0.1);
}

/* 主内容 */
.main-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  position: relative;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  overflow: hidden;
}

.bg-circle {
  position: absolute;
  border-radius: 50%;
  background-color: rgba(254, 44, 85, 0.05);
}

.bg-circle-1 {
  width: 500px;
  height: 500px;
  top: -100px;
  right: -100px;
}

.bg-circle-2 {
  width: 300px;
  height: 300px;
  bottom: -50px;
  left: -50px;
  background-color: rgba(54, 162, 235, 0.05);
}

/* 注册卡片 */
.register-card {
  background-color: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 500px;
  padding: 40px;
  position: relative;
  z-index: 1;
  transition: all var(--transition-duration) ease;
}

.register-card:hover {
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.15);
}

.card-header {
  text-align: center;
  margin-bottom: 30px;
}

.card-title {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 10px;
}

.card-subtitle {
  color: var(--gray-color);
  font-size: 16px;
}

/* 社交登录 */
.social-login {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-bottom: 30px;
}

.social-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #EEEEEE;
  cursor: pointer;
  transition: all var(--transition-duration) ease;
  position: relative;
}

.social-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
}

.social-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: 50%;
  opacity: 0;
  transition: opacity var(--transition-duration) ease;
}

.social-btn:hover::before {
  opacity: 0.1;
}

.social-btn.weixin::before {
  background-color: #07C160;
}

.social-btn.weibo::before {
  background-color: #E6162D;
}

.social-btn.qq::before {
  background-color: #12B7F5;
}

.social-btn.alipay::before {
  background-color: #00A0E9;
}

.social-btn i {
  font-size: 20px;
  position: relative;
  z-index: 1;
}

.social-btn.weixin i {
  color: #07C160;
}

.social-btn.weibo i {
  color: #E6162D;
}

.social-btn.qq i {
  color: #12B7F5;
}

.social-btn.alipay i {
  color: #00A0E9;
}

/* 分割线 */
.divider {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}

.divider-line {
  flex: 1;
  height: 1px;
  background-color: #EEEEEE;
}

.divider-text {
  padding: 0 20px;
  color: var(--gray-color);
  font-size: 14px;
}

/* 表单样式 */
.register-form .el-form-item {
  margin-bottom: 24px;
}

.register-form .el-input__inner {
  height: 48px;
  border-radius: 8px;
  border: 1px solid #EEEEEE;
  padding: 0 16px;
  font-size: 16px;
  transition: all var(--transition-duration) ease;
}

.register-form .el-input__inner:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(254, 44, 85, 0.1);
}

.register-form .el-form-item__label {
  font-size: 14px;
  font-weight: 500;
  color: #333333;
  margin-bottom: 8px;
}

.register-form .el-form-item__error {
  padding-top: 4px;
}

.register-form .el-checkbox__label {
  font-size: 14px;
  color: var(--gray-color);
}

.register-form .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
}

.register-form .el-checkbox__input.is-focus .el-checkbox__inner {
  box-shadow: 0 0 0 2px rgba(254, 44, 85, 0.1);
}

.register-form .el-checkbox__inner:hover {
  border-color: var(--primary-color);
}

.register-btn {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  background-color: var(--primary-color);
  color: white;
  font-size: 16px;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: all var(--transition-duration) ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-btn:hover {
  background-color: #FF4D6D;
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(254, 44, 85, 0.2);
}

.register-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 5px rgba(254, 44, 85, 0.2);
}

.register-btn:disabled {
  background-color: rgba(254, 44, 85, 0.5);
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

.terms-link {
  color: var(--primary-color);
  text-decoration: none;
}

.terms-link:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 576px) {
  .register-card {
    padding: 30px 20px;
  }

  .card-title {
    font-size: 24px;
  }

  .card-subtitle {
    font-size: 14px;
  }

  .social-login {
    gap: 15px;
  }

  .social-btn {
    width: 40px;
    height: 40px;
  }

  .social-btn i {
    font-size: 16px;
  }

  .register-form .el-input__inner {
    height: 44px;
    font-size: 14px;
  }

  .register-btn {
    height: 44px;
    font-size: 14px;
  }
}
</style>
